package io.bdmc.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年12月21日 下午12:53:33
 */
public class DateUtils {
    /** 时间格式(yyyy-MM-dd) */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    public final static String TIME_PATTERN = "HH:mm:ss";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /** 时间格式(yyyyMMddHHmmss) */
    public final static String FILE_DATE_PATTERN = "yyyyMMddHHmmss";

    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 获取当前时间 yyyy-MM-dd HH:mm:ss
     */
    public static String getNowLocalDateTime() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
        String dateString = LocalDateTime.now().format(formatter);
        return dateString;
    }

    /**
     * 格式化LocalDateTime 为 指定pattern格式
     */
    public static String formatLocalDateTime(LocalDateTime date, String pattern) {
        if (date != null) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(pattern);
            return date.format(df);
        }
        return null;
    }

    public static Date stringToDate(String date) throws ParseException {
        java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = format1.parse(date);
        return d;
    }

    public static LocalDateTime stringToLocalDateTime(String date) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(date, df);
        return ldt;
    }

    public static LocalDateTime timestamToDatetime(String timestamp){
        Long time = Long.valueOf(timestamp);
        Instant instant = Instant.ofEpochMilli(time);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }


    public static Date dateAddMinute(Date date, int minute) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MINUTE, minute);
        Date dt1 = rightNow.getTime();
        return dt1;
    }

}
